Skip to content

Added forward euler in python3 #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 2, 2018
Merged

Added forward euler in python3 #203

merged 3 commits into from
Jul 2, 2018

Conversation

sklan
Copy link
Contributor

@sklan sklan commented Jul 2, 2018

No description provided.

@sklan sklan changed the title Added forward_euler in python Added forward euler in python3 Jul 2, 2018
@Gathros Gathros added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Jul 2, 2018
Copy link
Contributor

@Butt4cak3 Butt4cak3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for another contribution!

First of all, you should add a code import to euler.md or else your code won't show up at all.

{% sample lang="py" %}
### Python
[import, lang:"python"](code/python/euler.py)

Then I found a few consistency things and one actual error in your code, but nothing you won't be able to fix.

def forward_euler(time_step, n):
result = [0] * n
result[0] = 1
for i in range(1, n): result[i] = result[i - 1] - 3.0 * result[i - 1] * time_step
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd put the loop header and the result[i] = ... statement on two separate lines.

def forward_euler(time_step, n):
result = [0] * n
result[0] = 1
for i in range(1, n): result[i] = result[i - 1] - 3.0 * result[i - 1] * time_step
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 3.0 instead of just 3?

return approx


time_step = 0.01
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the Python code we have in the archive uses a main() function. To keep everything consistent, you should put this stuff inside a def main(): and call main() like, for example, in this file.

if abs(result[i] - solution) > threshold:
print(result[i], solution)
approx = False
return approx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your return statement is inside the for-loop.

@Butt4cak3 Butt4cak3 self-assigned this Jul 2, 2018
@Butt4cak3
Copy link
Contributor

That should do it!

@Butt4cak3 Butt4cak3 merged commit c35ed42 into algorithm-archivists:master Jul 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants